package com.example.demo.controller;

import com.example.demo.entity.CourseBean;
import com.example.demo.service.CourseService;
import com.example.demo.util.PageEntity;
import com.example.demo.util.ResponseEntity;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
@Api(tags = "课程管理")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody CourseBean courseBean) {
        Integer insert = courseService.add(courseBean);
        if(insert > 0) {
            return ResponseEntity.success("新增成功");
        } else {
            return ResponseEntity.failed("新增失败");
        }
    }

    @PostMapping("/edit")
    public ResponseEntity edit(@RequestBody CourseBean courseBean) {
        Integer update = courseService.update(courseBean);
        if(update > 0) {
            return ResponseEntity.success("修改成功");
        } else {
            return ResponseEntity.failed("修改失败");
        }
    }

    @GetMapping("/del")
    public ResponseEntity del(@RequestParam("id") Integer id) {
        Integer delete = courseService.delete(id);
        if(delete > 0) {
            return ResponseEntity.success("删除成功");
        } else {
            return ResponseEntity.failed("删除失败");
        }
    }

    @GetMapping("/get/{id}")
    public ResponseEntity getOne(@PathVariable Integer id) {
        CourseBean one = courseService.one(id);
        ResponseEntity re = ResponseEntity.success("获取成功");
        re.setData(one);
        return re;
    }

    @GetMapping("/list")
    public ResponseEntity list(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "sort", defaultValue = "id") String sort,
            @RequestParam(value = "order", defaultValue = "desc") String order
    ) {
        String sortOrder = sort + " " + order;
        PageEntity list = courseService.list(pageNo, pageSize, name, sortOrder);
        return new ResponseEntity<>(200, null, list);
    }
} 